﻿using System;
using System.Collections.Generic;
using System.Text;

using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Core;
using OutlookClickToCallAddin;

namespace ClickToCall.ClickToCallToolbar.Builders
{
    public class OutlookContactFilter : ContactFilter
    {
        public OutlookContactFilter(bool useHome, bool useMobile, bool useBusiness, bool useBusiness2,
            bool useOther,bool useCallback,bool useCar,bool useCompany,bool usePrimary)
        {
            this.UseHome = useHome;
            this.UseMobile = useMobile;
            this.UseBusiness = useBusiness;
            this.UseBusiness2 = useBusiness2;
            this.UseOther = useOther;
            this.UseCallback = useCallback;
            this.UseCar = useCar;
            this.UseCompany = useCompany;
            this.UsePrimary = usePrimary;
        }
        public readonly bool UseHome;
        public readonly bool UseMobile;
        public readonly bool UseBusiness;
        public readonly bool UseBusiness2;
        public readonly bool UseOther;
        public readonly bool UseCallback;
        public readonly bool UseCar;
        public readonly bool UseCompany;
        public readonly bool UsePrimary;
    }
    public class OutlookContactBuilder : ContactBuilder
    {
        private OutlookContactFilter _filter;
        public OutlookContactBuilder(OutlookContactFilter filter)
            : base(filter)
        {
            _filter = filter;
        }
        /// <summary>
        /// Return a ContactCollection of Contacts found within a local instance of Outlook 2007
        /// </summary>
        /// <returns>ContactCollection</returns>
        /// <exception cref="ContactBuilderException">Thrown if an error occurs while building contact list.</exception> 
        public override ContactCollection GetContacts()
        {
            ContactCollection cc = new ContactCollection();
            //If outlook is not installed, then do not attempt to process contacts
            if (!IsBuilderAvailable(this.AppName))
                return cc;

            Outlook.Application _outlookApp;
            try
            {
                _outlookApp = new Outlook.ApplicationClass();
                COMAddIns addins = _outlookApp.COMAddIns;
                object clickToCall = null;
                string progid = string.Empty;
                foreach (COMAddIn addin in addins)
                {
                    /*
                     * This GUID references the ClickToCallOutlookAddin Add-in
                     */
                    if (addin.Guid == "{BF714AFD-E20B-4531-8C86-FB42C0CC96E9}")
                    {
                        clickToCall = addin.Object;
                        progid = addin.ProgId;
                        break;
                    }
                }

                if (clickToCall != null)
                {
                    IClickToCallContacts ccAddin = (IClickToCallContacts)clickToCall;
                    IClickToCallContact[] contacts = ccAddin.GetLocalContacts();

                    PhoneNumbers pns;
                    for (int i = 0; i < contacts.Length; i++)
                    {
                        IClickToCallContact contact =
                            (IClickToCallContact)contacts[i];

                        pns = new PhoneNumbers();
                        if (_filter.UseBusiness2) pns.Business2TelephoneNumber = contact.Business2TelephoneNumber;
                        if (_filter.UseBusiness)  pns.BusinessTelephoneNumber = contact.BusinessTelephoneNumber;
                        if (_filter.UseCallback)  pns.CallbackTelephoneNumber = contact.CallbackTelephoneNumber;
                        if (_filter.UseCar)       pns.CarTelephoneNumber = contact.CarTelephoneNumber;
                        if (_filter.UseCompany)   pns.CompanyMainTelephoneNumber = contact.CompanyMainTelephoneNumber;
                        if (_filter.UseHome)      pns.HomeTelephoneNumber = contact.HomeTelephoneNumber;
                        if (_filter.UseMobile)    pns.MobileTelephoneNumber = contact.MobileTelephoneNumber;
                        if (_filter.UseOther)     pns.OtherTelephoneNumber = contact.OtherTelephoneNumber;
                        if (_filter.UsePrimary)   pns.PrimaryTelephoneNumber = contact.PrimaryTelephoneNumber;

                        cc.Add(new Contact(pns, contact.ContactName, contact.IMAddress, contact.EmailAddress));

                    }
                }
            }
            catch (Exception e)
            {
                throw new ContactBuilderException(this, e);
            }
            finally
            {
                _outlookApp = null;
            }
            return cc;
        }

        public override string AppName
        {
            get { return "outlook.exe"; }
        }
    }
}
